package com.bawei.mdm2207alast.report

import android.annotation.SuppressLint
import android.util.Log
import androidx.compose.foundation.background
import androidx.compose.foundation.border
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.grid.GridCells
import androidx.compose.foundation.lazy.grid.LazyVerticalGrid
import androidx.compose.foundation.lazy.grid.items
import androidx.compose.foundation.lazy.items
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Add
import androidx.compose.material3.AlertDialog
import androidx.compose.material3.Badge
import androidx.compose.material3.Card
import androidx.compose.material3.CardColors
import androidx.compose.material3.Icon
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.runtime.snapshots.SnapshotStateList
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.colorResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.navigation.NavController
import androidx.navigation.compose.rememberNavController
import com.bawei.mdm2207alast.R
import com.bawei.mdm2207alast.base.OnError
import com.bawei.mdm2207alast.base.OnLoading
import com.bawei.mdm2207alast.base.OnSuccess
import com.bawei.mdm2207alast.common.BaseTitle
import com.bawei.mdm2207alast.model.SelReportEntity
import com.bawei.mdm2207alast.route.RouteConfig
import com.blankj.utilcode.util.ToastUtils
import com.google.gson.Gson
import kotlinx.coroutines.launch
import java.text.SimpleDateFormat

/**
 * 作者：赵亮
 * 时间：2024/12/16 15:36
 * 功能：查询工作日志页面
 */
@SuppressLint("UnusedMaterial3ScaffoldPaddingParameter")
@Composable
fun SelReportPage(
    navController: NavController = rememberNavController(),
    viewModel: ReportViewModel = hiltViewModel()
) {

    // 1. 数据
    val list = remember {
        SnapshotStateList<SelReportEntity.Data>()
    }
    LaunchedEffect(key1 = "") {
        launch {
            viewModel.uiState.collect {
                when (it) {
                    is OnError -> {
                        ToastUtils.showShort(it.msg)
                        Log.i("TAG", "SelReportPage: ${it.msg}")
                    }

                    OnLoading -> {}
                    is OnSuccess<*> -> {
                        list.clear()
                        list.addAll((it.data as SelReportEntity).datas.reversed())
                    }
                }
            }
        }
        launch {
            viewModel.sendIntent(ReportIntent.SelReport(mapOf("userId" to 1, "page" to 118)))
        }
    }

    // 是否显示添加工作日志对话框
    var isShow by remember {
        mutableStateOf(false)
    }
    val list1 = listOf(
        ReportEntity(1, "1", "日报"),
        ReportEntity(2, "7", "周报"),
        ReportEntity(3, "30", "月报"),
        ReportEntity(4, "季度", "季度报表"),
        ReportEntity(5, "业绩", "业绩报表"),
        ReportEntity(6, "营业", "营业财报"),
    )

    Scaffold(
        floatingActionButton = {
            Icon(
                imageVector = Icons.Default.Add,
                contentDescription = null,
                modifier = Modifier.clickable {
                    isShow = true
                })
        }
    ) {
        Column {
            BaseTitle(navController = navController, title = "工作日志")

            // 展示
            LazyColumn(modifier = Modifier.padding(start = 15.dp)) {
                items(list) {
                    Column(verticalArrangement = Arrangement.spacedBy(10.dp)) {
                        Row(verticalAlignment = Alignment.CenterVertically) {
                            Badge(modifier = Modifier.size(30.dp), containerColor = Color.Green)
                            Spacer(modifier = Modifier.width(10.dp))
                            Text(
                                text = listOf(
                                    "",
                                    "日报",
                                    "周报",
                                    "月报",
                                    "季度报表",
                                    "业绩报表",
                                    "营业财报"
                                )[it.reportType]
                            )
                        }
                        Column(
                            modifier = Modifier.padding(start = 10.dp),
                            verticalArrangement = Arrangement.spacedBy(10.dp)
                        ) {
                            Text(text = it.reportNowTitle)
                            Text(text = it.reportNowValue)
                            Text(text = it.reportNextTitle)
                            Text(text = it.reportNextValue)
                            Text(text = "${SimpleDateFormat("yyyy年MM月dd日").format(it.reportTime)}  发布")
                        }

                    }
                }
            }
        }
    }
    if (isShow) {
        AlertDialog(
            title = { Text(text = "新建汇报") },
            text = {
                LazyVerticalGrid(columns = GridCells.Fixed(3)) {
                    items(list1) {
                        Column(modifier = Modifier.clickable {
                            // 跳转到新建工作汇报页面
                            navController.navigate(RouteConfig.ADD_REPORT + "/${Gson().toJson(it)}")
                            isShow = false
                        }, horizontalAlignment = Alignment.CenterHorizontally) {
                            Badge(
                                modifier = Modifier.size(50.dp),
                                containerColor = colorResource(id = R.color.add_report_bg)
                            ) {
                                Box(
                                    modifier = Modifier
                                        .background(color = Color.White)
                                        .size(40.dp)
                                        .border(
                                            width = 2.dp,
                                            color = Color.Blue,
                                            shape = RoundedCornerShape(10.dp)
                                        )
                                ) {
                                    Text(
                                        text = it.name,
                                        color = Color.Blue,
                                        modifier = Modifier.align(Alignment.Center)
                                    )
                                }
                            }
                            Text(text = it.title)
                        }
                    }
                }
            },
            onDismissRequest = { }, confirmButton = { })
    }
}

/**
 * 添加工作日志的对话框中网格数据类
 * @param type 类型
 * @param name 名字
 * @param title 标题
 */
data class ReportEntity(val type: Int, val name: String, val title: String)

@Preview(showSystemUi = true)
@Composable
fun SelReportPagePreview() {
    SelReportPage()
}

